package de.java.shortys.myhome.fuelmonitor.model;

import java.util.List;

import de.java.shortys.myhome.fuelmonitor.controller.FuelMonitorController;
import de.java.shortys.myhome.keymanager.controller.KeyManager;

public class FuelMonitorService implements IFuelMonitorService {

	private IFuelMonitorStorageService fuelMonitorDao; // per Spring

	private IFuelMonitorModelService fuelMonitor;

	{
		fuelMonitor = new FuelMonitor();
	}

	@Override
	public void create(IFuelFilling fuelFilling) {
		fuelMonitorDao.create(fuelFilling);
		fuelMonitor.create(fuelFilling);
	}

	@Override
	public void delete(IFuelFilling fuelFilling) {
		fuelMonitorDao.delete(fuelFilling);
		fuelMonitor.delete(fuelFilling);
		KeyManager.getManager(FuelMonitorController.NAME).removeKey(fuelFilling.getKey());
	}

	@Override
	public IFuelMonitorModelService getFuelMonitor() {
		return fuelMonitor;
	}

	@Override
	public IFuelMonitorStorageService getFuelMonitorDao() {
		return fuelMonitorDao;
	}

	@Override
	public List<IFuelFilling> read() {
		return fuelMonitor.read();
	}

	public void setFuelMonitorDao(IFuelMonitorStorageService fuelMonitorDao) {
		this.fuelMonitorDao = fuelMonitorDao;
		fuelMonitor.setFuelFillings(fuelMonitorDao.read());
	}

	@Override
	public void update(IFuelFilling fuelFilling) {
		fuelMonitorDao.update(fuelFilling);
		fuelMonitor.update(fuelFilling);
	}

}
